/**
 * 
 */
package com.wzl.qs;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wzl.qs.db.DbAdapter;
import com.wzl.qs.db.ENote;

/**
 * @author g0800981
 * 
 */
public class NoteView extends Activity {
	private static final int MENU_ITEM_DELETE = 1;
	private static final int MENU_ITEM_FAVORATE = 2;
	private static final int ABOUT = 3;
	private static final int HELP = 4;

	private ImageView imageView;
	private TextView nameView;
	private TextView descView;
	private int note_id;
	private static int PHOTO_SIZE = 320;
	DbAdapter dbAdapter;
	MediaPlayer mp;
	Button btnPlay;
	Button btnStop;
	String audioPath;
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Need to create a new screen layout for viewing purpose
		// put views on the layout to show name, photo, and everything
		setContentView(R.layout.note_view);
		context = this;
		dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		imageView = (ImageView) findViewById(R.id.note_ImageView01);
		nameView = (TextView) findViewById(R.id.android_note_nameView);
		descView = (TextView) findViewById(R.id.android_note_descView);

		btnPlay = (Button) findViewById(R.id.btnNotePlayAudio);
		btnStop = (Button) findViewById(R.id.btnNoteStopAudio);

		mp = new MediaPlayer();

		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			note_id = extras.getInt(DbAdapter.NID_FIELD);
			ENote anote = dbAdapter.GetENote(note_id);
			dbAdapter.increasePopularity(note_id, 1);
			String name_text = anote.getName();
			String desc_text = anote.getDescription();
			String photoPath = anote.getPhotoPath();
			String inputdate = anote.getInputDate();
			audioPath = anote.getAudioPath();
			if (name_text != null) {
				nameView.setText(name_text
						+ " encourages you to quit smoking on " + inputdate
						+ ":");
			}
			if (descView != null) {
				descView.setText(desc_text);
			}
			if (photoPath.length() > 0) {
				try {
					Bitmap bm = BitmapUtil.getBitmap(photoPath, PHOTO_SIZE);
					imageView.setImageBitmap(bm);
				} catch (FileNotFoundException e) {
					Log.e("Error in NoteView", e.getMessage());
					e.printStackTrace();
				}
			} else {

			}

			if (audioPath.length() > 0) {
				btnPlay.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						playAudio(audioPath);
					}
				});

				btnStop.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mp != null) {
							mp.stop();
						}
					}
				});
			} else {
				btnPlay.setEnabled(false);
				btnPlay.setText("No Audio");
				btnStop.setEnabled(false);
//				btnStop.setText("No Audio");
			}
		}

	}

	private void playAudio(String audioPath) {
		try {
			mp = new MediaPlayer();
//			mp.setVolume((float)0.99, (float)0.99);
			mp.setDataSource(audioPath);
			mp.prepare();
			mp.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreatePanelMenu(int featureId, Menu menu) {
		MenuItem mi = menu
				.add(Menu.NONE, MENU_ITEM_DELETE, Menu.NONE, "Delete");
		mi.setIcon(android.R.drawable.ic_menu_delete);
		MenuItem miAbout = menu.add(Menu.NONE, ABOUT, Menu.NONE, "About");
		miAbout.setIcon(R.drawable.qsm);
		MenuItem mihelp = menu.add(Menu.NONE, HELP, Menu.NONE, "Help");
		mihelp.setIcon(android.R.drawable.ic_dialog_info);
		// menu.add
		// menu.add(Menu.NONE, MENU_ITEM_FAVORATE, Menu.NONE,
		// "Set as favorate");
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder = builder.setMessage("Are you sure to delete?");
		builder.setCancelable(true);
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// delete
				DbAdapter dba = new DbAdapter(context);
				dba.open();
				dba.DeleteENote(note_id);
				dba.close();
				finish();
			}
		});

		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();

			}
		});

		return builder.create();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int iid = item.getItemId();

		switch (iid) {
		case MENU_ITEM_DELETE:
			showDialog(0);
			break;
		case ABOUT:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;

		case HELP:
			Intent aboutIntent = new Intent(this, HelpNoteView.class);
			startActivity(aboutIntent);
			break;
		}

		// if (iid == MENU_ITEM_DELETE) {
		// showDialog(0);
		// }

		// if (iid == MENU_ITEM_FAVORATE) {
		// DbAdapter dba = new DbAdapter(context);
		// dba.open();
		// dba.increasePopularity(note_id, 10);
		// dba.close();
		// Toast.makeText(context, "We have increased .",
		// Toast.LENGTH_SHORT).show();
		// }

		return super.onOptionsItemSelected(item);
	}

}
